package com.jk.dailydemo.camera;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.jk.dailydemo.R;
import com.jk.dailydemo.RPResultListener;
import com.jk.dailydemo.RuntimePermissionUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/29.
 */

public class MyCamera extends AppCompatActivity {
    private String[] cameraPermiss = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
    private boolean isHavePower = false;
    private int CAMERA_REQUEST_CODE = 200;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_camera);
        ButterKnife.bind(this);
        checkPower();

    }

    private void checkPower(){
        boolean writePermission = RuntimePermissionUtil.checkPermissonGranted(this, cameraPermiss[1]);
        boolean cameraPermission = RuntimePermissionUtil.checkPermissonGranted(this, cameraPermiss[0]);
        if(!writePermission&&!cameraPermission){
            RuntimePermissionUtil.requestPermission(this,cameraPermiss,100);
            isHavePower = false;
            return ;
        }
        RuntimePermissionUtil.onRequestPermissionsResult(new int[]{100}, new RPResultListener() {
            @Override
            public void onPermissionGranted() {
                isHavePower = true;

            }

            @Override
            public void onPermissionDenied() {
                isHavePower = false;
            }
        });
        isHavePower = true;
    }

    @OnClick({R.id.btn_take_picture})
    public void onClick(){
        checkPower();

        if (isHavePower){
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());
            intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
            startActivityForResult(intent, CAMERA_REQUEST_CODE);
        }else{
            Toast.makeText(this,"请求相机，需开启相机权限",Toast.LENGTH_SHORT).show();
        }


    }

    private Uri getUri() {

        return Uri.fromFile(getFile());
    }

    /**
     * 该方法用于获取指定路径 和 名字 的file
     * @return
     */
    private File getFile() {
        File filePath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "CameraDemo");
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        //将图片保存的名字设置为当前拍照的时间
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        String name = format.format(new Date());
        File file = new File(filePath.getPath() + File.separator + name + ".jpg");
        return file;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CAMERA_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this,"拍照成功",Toast.LENGTH_SHORT).show();
                Log.d("print", "拍照成功");
            }
        }
    }







}
